package com.kust.smarhome.background.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kust.smarhome.background.common.dto.OrderFormInfoDto;
import com.kust.smarhome.background.common.lang.Result;
import com.kust.smarhome.background.common.vo.CourierVo;
import com.kust.smarhome.background.common.vo.OrderFormVo;
import com.kust.smarhome.background.model.OrderForm;
import com.kust.smarhome.background.service.IOrderFormService;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author iAuzre
 * @since 2021-07-31
 */
@RestController
@RequestMapping("/orderForm")
public class OrderFormController extends BaseController {

    @Resource
    private IOrderFormService orderFormService;

    @GetMapping("/list")
    public Result list(OrderFormVo orderFormVo, Principal principal) {

        if (getUserPower(principal.getName()))
            return Result.fail(401, "权限不足", null);

        Page<OrderFormInfoDto> page = orderFormService.getOrderFormInfo(getPage(), orderFormVo);

        return Result.succ(page);
    }

    @PostMapping("/delete")
    public Result deleteByIds(@RequestBody String[] ids, Principal principal) {

        if (getUserPower(principal.getName()))
            return Result.fail(401, "权限不足", null);

        for (String id : ids) {
            orderFormService.removeById(id);
        }

        return Result.succ(null);

    }

    @PostMapping("/setCourier")
    public Result setCourier(@RequestBody @Validated CourierVo courierVo, BindingResult result, Principal principal) {

        if (result.hasErrors())
            return Result.fail(result.getFieldError().getDefaultMessage());

        OrderForm orderForm = orderFormService.getById(courierVo.getId());
        if (orderForm.getState() != 1)
            return Result.fail("状态异常,无法录入");

        orderForm.setCourierNumber(courierVo.getCourier())
                .setState(2);
        if (!orderFormService.updateById(orderForm))
            return Result.fail("系统异常!");
        return Result.succ(null);
    }

    @GetMapping("/getState0Num")
    public Result getState0Num() {

        QueryWrapper<OrderForm> query = new QueryWrapper<>();
        query.eq("state", 0);
        List<OrderForm> list = orderFormService.list(query);

        return Result.succ(list.size());
    }

    @GetMapping("/getState1Num")
    public Result getState1Num() {

        QueryWrapper<OrderForm> query = new QueryWrapper<>();
        query.eq("state", 1);
        List<OrderForm> list = orderFormService.list(query);

        return Result.succ(list.size());
    }

    @GetMapping("/getState2Num")
    public Result getState2Num() {

        QueryWrapper<OrderForm> query = new QueryWrapper<>();
        query.eq("state", 2);
        List<OrderForm> list = orderFormService.list(query);

        return Result.succ(list.size());
    }

    @GetMapping("/getState3Num")
    public Result getState3Num() {

        QueryWrapper<OrderForm> query = new QueryWrapper<>();
        query.eq("state", 3);
        List<OrderForm> list = orderFormService.list(query);

        return Result.succ(list.size());
    }

    @GetMapping("/timeTask")
    public Result getTimeTask() {

        List<OrderForm> orderForms = orderFormService.list();
        return Result.succ(orderForms.size());
    }


}
